package cn.zktr.Personnel.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

//日期转换器
public class DateConverter implements Converter<String,Date>{

	@Override
	public Date convert(String source) {
		SimpleDateFormat sdf = null;
		if("".equals(source) && source== null) {
			return null;
		}
		if(source.matches("\\d{4}-\\d{1,2}-\\d{1,2}[^A-Za-z]*")) {
			if(source.matches("[^A-Za-z]+\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
				sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			}else {
				sdf = new SimpleDateFormat("yyyy-MM-dd");
			}
		}else if(source.matches("\\d{4}/\\d{1,2}/\\d{1,2}[^A-Za-z]*")) {
			if(source.matches("[^A-Za-z]+\\d{1,2}:\\d{1,2}:\\d{1,2}")) {
				sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
			}else {
				sdf = new SimpleDateFormat("yyyy/MM/dd");
			}
		}else {
			return null;
		}
		try {
			return sdf.parse(source);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	
}
